package org.outlook.apps.goal.ui;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.outlook.apps.goal.pojo.Goal;
import org.outlook.commons.io.ResourceUtil;
import org.outlook.commons.pojo.Globals;

public class ViewUI extends JFrame implements Runnable, ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel viewPanel;
	private JButton addBt;
	private JButton editBt;
	private JButton delBt;
	private JTable table;
	private DefaultTableModel dm;

	public ViewUI() throws HeadlessException {
		super();
		setDefaultLookAndFeelDecorated(Boolean.FALSE);
		setLayout(new BorderLayout());
	}

	@Override
	public void run() {
		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.NORTH);

		addBt = createTool("Add", "/images/add.gif");
		editBt = createTool("Edit", "/images/edit.gif");
		delBt = createTool("Delete", "/images/delete.gif");
		toolBar.add(addBt);
//		toolBar.addSeparator();
		toolBar.add(editBt);
//		toolBar.addSeparator();
		toolBar.add(delBt);
		toolBar.setFloatable(Boolean.FALSE);

		dm = new DefaultTableModel(getGoals(), new String[]{"Index","","Title","Description","Create At","Sequence"});
//		dm.addColumn(null);
//		dm.addColumn(null);
//		dm.addColumn("Title");
//		dm.addColumn("Description");
//		dm.addColumn("Create At");
//		dm.addColumn("Sequence");
//		dm.addRow(getGoals());
		table = new JTable(dm);
		final TableColumnModel cm = table.getColumnModel();
		cm.getColumn(0).setPreferredWidth(25);
		final TableColumn checkBoxColumn = cm.getColumn(1);
		checkBoxColumn.setCellEditor(table.getDefaultEditor(Boolean.class));
		checkBoxColumn.setCellRenderer(table.getDefaultRenderer(Boolean.class));
		checkBoxColumn.setPreferredWidth(20);
		cm.getColumn(2).setPreferredWidth(100);
		cm.getColumn(3).setPreferredWidth(300);
		cm.getColumn(4).setPreferredWidth(150);
		cm.getColumn(5).setPreferredWidth(60);
		viewPanel = new JPanel();
		viewPanel.add(table);
		JScrollPane editPane = new JScrollPane(viewPanel);
		add(editPane, BorderLayout.CENTER);
		setVisible(Boolean.TRUE);
		pack();
	}

	private JButton createTool(String text, String icon) {
		JButton bt = new JButton("", new ImageIcon(ResourceUtil.getResourceInClasspath(icon)));
		bt.setToolTipText(text);
		return bt;
	}

	private Object[][] getGoals() {
		final Collection<Goal> goals = Globals.dbService.getGoalList();
		if(null != goals) {
			Object[][] objs = new Object[goals.size()][];
			int i = 0;;
			for(Goal goal:goals) {
				objs[i++] = new Object[]{i,Boolean.FALSE, goal.getTitle(), goal.getDescription(), goal.getCreateTimeStamp(), goal.getSequence()};
			}
			return objs;
		}
		return null;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args) {
		new Thread(new ViewUI()).start();		
	}

}
